import React from 'react';
import { connect } from 'dva';
import { Link, browserHistory } from 'dva/router';
import { Select } from 'antd';
const R = require('ramda');

const Option = Select.Option;

class BusinessParams extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { dispatch, sprCode } = this.props;
    dispatch({ type: 'customsInfoGenerate/getBusinessParamsByCode', sprCode });
  }

  render() {
    const {
      all,
      sprCode,
      disabledKeys,
      disabled,
      placeholder,
      style,
      mode,
      customsInfoGenerate,
    } = this.props;
    let allList = R.isNil(all) ? false : all;
    let list = R.isNil(customsInfoGenerate.bizParams[sprCode])
      ? []
      : customsInfoGenerate.bizParams[sprCode];
    if (allList) list = R.concat([{ sprName: '', sprValue: '全部' }])(list);
    let keys = R.isNil(disabledKeys) ? [] : disabledKeys;

    return (
      <Select
        disabled={disabled}
        showSearch
        {...R.omit(['customsInfoGenerate'])(this.props)}
        placeholder={placeholder ? placeholder : '可通过输入代码或关键字搜索'}
        filterOption={(input, option) =>
          option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0 ||
          option.props.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
        }
        style={style}
        mode={mode}
        allowClear
      >
        {list.map(item => {
          return (
            <Option
              value={item.sprName}
              key={R.isEmpty(item.sprName) ? 'all' : item.sprName}
              disabled={R.contains(item.sprName, keys)}
            >
              {item.sprValue}
            </Option>
          );
        })}
      </Select>
    );
  }
}

export default connect(({ customsInfoGenerate }) => ({ customsInfoGenerate }))(BusinessParams);
